/*
 * @File:
 * @Author: sjk
 * @Date: 2022-03-07 17:42:45
 * @LastEditTime 2022-04-08 21:00:14
 * @Brief:
 * @Note:   魔改编码器
 *          使用引脚
 *  TIM3:
 *  PB4 PB5       龙邱母板 编码器2
 *  TIM4
 *  PB6 PB7       龙邱母板 编码器4
 */
#include "include.h"
#include "Encoder.h"
#include "Global_define.h"

/**
 * @Brief:初始化两个编码器 使用TIM3 TIM4
 * @param {*}
 * @return {*}
 * @Note:
 *              魔改自EncInit()
 */
void Encoder_myInit()
{
    
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4, ENABLE);

    GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_2);                             //管脚复用AF2
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_2);                             //管脚复用AF2
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_2);                             //管脚复用AF2
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_2);                             //管脚复用AF2
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; // PB4,5,6,7
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;                            //输入，悬空
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    //编码器3
    TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
    TIM_TimeBaseStructure.TIM_Prescaler = 1;                                                              //预分频器
    TIM_TimeBaseStructure.TIM_Period = 0xFFFF;                                                            //设定计数器自动重装值
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;                                               //选择时钟分频：不分频
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;                                           // TIM向上计数
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;                                                      //重复计数器
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);                                                       //初始化结构体
    TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); //使用编码器模式为TI12支持带龙-邱方向的编码器
    TIM_Cmd(TIM3, ENABLE);                                                                                //定时器使能

    //编码器4
    TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
    TIM_TimeBaseStructure.TIM_Prescaler = 1;                                                              //预分频器
    TIM_TimeBaseStructure.TIM_Period = 0xFFFF;                                                            //设定计数器自动重装值
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;                                               //选择时钟分频：不分频
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;                                           // TIM向上计数
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;                                                      //重复计数器
    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);                                                       //初始化结构体
    TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); //使用编码器模式为TI12支持带龙-邱方向的编码器
    TIM_Cmd(TIM4, ENABLE);                                                                                //定时器使能
}

/**
 * @Brief: 测试编码器
 * @param {*}
 * @return {*}
 * @Note:
 */
void Test_Encoder()
{
    Encoder_myInit();
    while (1)
    {
        OLEDsprint(0, 2, "left :%05d", Enc_left());
        OLEDsprint(0, 4, "right:%05d", Enc_right());
        delayms(200);
    }
}